package kuba.gui;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import kuba.bean.Author;
import kuba.bean.CD;
import kuba.common.Messages;
import kuba.service.Service;

import org.springframework.beans.factory.annotation.Autowired;

public class ButtonListener implements ActionListener, ListSelectionListener, ChangeListener{
    @Autowired
    private Service service;

    @Autowired 
    private MainWindow window;

    private AuthorCdGui authorCdGui = new AuthorCdGui();

    @Override
    public void actionPerformed(ActionEvent event) {
        //Authors
        if (event.getSource() == window.btnAuthorGetAll) {
            getAllAuthors();
        }
        if (event.getSource() == window.btnAuthorCd) {
            getAuthorCds();
        }

        if (event.getSource() == window.btnAuthorAdd) {
            addAuthor();
            getAllAuthors();
        }
        if (event.getSource() == window.btnAuthorDelete) {
            deleteAuthor();
            getAllAuthors();
        }
        if (event.getSource() == window.btnAuthorUpdate) {
            updateAuthor();
            getAllAuthors();
        }
        //CD's
        if (event.getSource() == window.btnCdGetAll) {
            getAllCd();
        }
        if (event.getSource() == window.btnCdAdd) {
            addCd();
            getAllCd();
        }
        if (event.getSource() == window.btnCdDelete) {
            deleteCd();
            getAllCd();
        }
        if (event.getSource() == window.btnCdUpdate) {
            updateCd();
            getAllCd();
        }
    }

    @Override
    public void valueChanged(ListSelectionEvent event) {
        if (event.getSource() == window.listAuthor) {
            fillAuthorFields();
        }
        if (event.getSource() == window.listCd) {
            fillCdFields();
        }

    }

    @Override
    public void stateChanged(ChangeEvent e) {
        if (e.getSource() == window.tabbedPane) {
            JTabbedPane pane = (JTabbedPane)e.getSource();
            if (pane.getSelectedIndex() == 1) {
                fillAuthorsCombo();
                getAllCd();
            }else {
                getAllAuthors();
            }
        }
    }


    @SuppressWarnings({ "rawtypes", "unchecked" })
    private void fillListModel(DefaultListModel listModel, Iterator iterator) {
        while(  iterator.hasNext()) {
            listModel.addElement(iterator.next());
        }
    }

    //Authors
    private void getAllAuthors() {

        DefaultListModel<Author> listModel = new DefaultListModel<Author>();

        Collection<Author> authors = service.getAllAuthors();
        Iterator<Author> iterator = authors.iterator();
        fillListModel(listModel, iterator);

        window.listAuthor.setModel(listModel);
    }

    private void getAuthorCds() {

        Author author = window.listAuthor.getSelectedValue();
        if (author == null) {
            JOptionPane.showMessageDialog(window, Messages.getString("ButtonListener.chooseAuthor")); //$NON-NLS-1$
        }else {
            Collection<CD> cds =service.getAuthorCds(author);

            DefaultListModel<CD> listModel = new DefaultListModel<CD>();
            Iterator<CD> iterator = cds.iterator();

            while(  iterator.hasNext() ) {
                listModel.addElement(iterator.next());

            }

            authorCdGui.list.setModel(listModel);

            authorCdGui.setLocationRelativeTo(null);
            authorCdGui.setVisible(true);
        }

    }

    private void fillAuthorFields() {
        Author author = window.listAuthor.getSelectedValue();
        if (author !=null) {
            window.txtAuthorName.setText(author.getName());
            window.txtAuthorLastName.setText(author.getLastName());
        }else {
            window.txtAuthorName.setText(""); //$NON-NLS-1$
            window.txtAuthorLastName.setText(""); //$NON-NLS-1$
        }
    }

    private void addAuthor() {
        Author author = new Author();
        author.setName(window.txtAuthorName.getText());
        author.setLastName(window.txtAuthorLastName.getText());
        service.insertAuthor(author);
    }

    private void deleteAuthor() {
        List<Author>temp = window.listAuthor.getSelectedValuesList();
        if (temp.isEmpty()) {
            JOptionPane.showMessageDialog(window, Messages.getString("ButtonListener.chooseAuthor")); //$NON-NLS-1$
        }else {
            for (Author author:temp) {
                service.deleteAuthor(author);
            }
        }
    }

    private void updateAuthor () {
        Author author=window.listAuthor.getSelectedValue();
        if (author == null) {
            JOptionPane.showMessageDialog(window, Messages.getString("ButtonListener.chooseAuthor")); //$NON-NLS-1$
        }else {

            author.setName(window.txtAuthorName.getText());
            author.setLastName(window.txtAuthorLastName.getText());
            service.updateAuthor(author);
        }

    }
    //CD's
    private void fillAuthorsCombo () {

        window.comboCdAuthor.removeAllItems();
        Iterator<Author> iterator = service.getAllAuthors().iterator();

        while (iterator.hasNext()) {
            window.comboCdAuthor.addItem(iterator.next());
        }

    }
    private void fillCdFields() {
        CD cd = window.listCd.getSelectedValue();
        if (cd != null) {
            window.txtCdTitle.setText(cd.getTitle());
            window.txtCdGenere.setText(cd.getGenere());
            for (int i = 0; i < window.comboCdAuthor.getItemCount(); i++ ) {

                if (window.comboCdAuthor.getItemAt(i).getId() == cd.getAuthorId()) {
                    window.comboCdAuthor.setSelectedIndex(i);
                }
            }
        }else {
            window.txtCdTitle.setText(""); //$NON-NLS-1$
            window.txtCdGenere.setText(""); //$NON-NLS-1$
        }
    }
    private void getAllCd() {
        DefaultListModel<CD> listModel = new DefaultListModel<CD>();
        Iterator <CD> iterator = service.getAllCds().iterator();

        fillListModel(listModel, iterator);
        window.listCd.setModel(listModel);

    }
    private void addCd () {
        Author author = (Author)window.comboCdAuthor.getSelectedItem();
        if (author == null) {
            JOptionPane.showMessageDialog(window, Messages.getString("ButtonListener.chooseAuthor")); //$NON-NLS-1$
        }else {

            CD cd = new CD ();
            cd.setTitle(window.txtCdTitle.getText());
            cd.setGenere(window.txtCdGenere.getText());

            service.insertCd(cd, author);
        }
    }
    private void deleteCd() {
        List<CD> temp = window.listCd.getSelectedValuesList();
        if (temp.isEmpty()) {
            JOptionPane.showMessageDialog(window, Messages.getString("ButtonListener.chooseCD")); //$NON-NLS-1$
        }else {
            for(CD cd:temp) {
                service.deleteCd(cd);
            }
        }
    }

    private void updateCd () {
        CD cd = window.listCd.getSelectedValue();
        if (cd == null) {
            JOptionPane.showMessageDialog(window, Messages.getString("ButtonListener.chooseCD")); //$NON-NLS-1$
        }else {
            cd.setTitle(window.txtCdTitle.getText());
            cd.setGenere(window.txtCdGenere.getText());
            Author author = (Author)window.comboCdAuthor.getSelectedItem();
            service.updateCd(cd, author);
        }

    }

    //Setters & getters
    public AuthorCdGui getAuthorCdGui() {
        return authorCdGui;
    }


    public void setAuthorCdGui(AuthorCdGui authorCdGui) {
        this.authorCdGui = authorCdGui;
    }


    public Service getService() {
        return service;
    }


    public void setService(Service service) {
        this.service = service;
    }


    public MainWindow getWindow() {
        return window;
    }


    public void setWindow(MainWindow window) {
        this.window = window;
    }





}
